package com.wimcorp.magic.data.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import com.wimcorp.magic.data.CardData;
@Transactional
public class CardDataDaoImpl implements CardDataDao {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public long saveOrUpdate(CardData o) {
		if (o.getId() == null) {
			entityManager.persist(o);
			return o.getId();
		} else {
			long id = entityManager.merge(o).getId();
			return id;
		
		}
	}
	
	public CardData getCardData(long multiverseid) {
		List<CardData> cards = entityManager
				.createQuery(
						"select c from CardData c where multiverseid=:multiverseid",
						CardData.class).setParameter("multiverseid", multiverseid).getResultList();
		if (cards.size() > 0) {
			return cards.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<CardData> getCards(String code) {
		List<CardData> cards = entityManager
				.createQuery(
						"select cd from DeckData dd join dd.cards cd where dd.code=:code order by cd.name",
						CardData.class).setParameter("code", code).getResultList();
		return cards;
	}

	

}
